package com.pengpai.pangucms.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.pengpai.pangucms.entity.SysUser;
import com.pengpai.pangucms.service.ISysUserService;
import com.pengpai.pangucms.utils.IPUtils;
import com.pengpai.pangucms.utils.UserUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.time.LocalDateTime;

@Slf4j
@Controller
public class AdminController {

    @Resource
    private ISysUserService sysUserService;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 登录页面跳转
     *
     * @param error 携带错误标志
     * @param model 数据题
     * @return 返回视图页面
     */
    @GetMapping("/login")
    public String login(String error, Model model, HttpServletRequest request) {

        //判断是否登录，如果是不返回登录页面
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            return "index";
        }

        //跳转登录页面
        model.addAttribute("error", error);
        return "login";
    }

    @GetMapping("/")
    public String index(HttpServletRequest request,Model model){

        //记录登录时间IP
        String ip = IPUtils.getIpAddr(request);
        SysUser sysUser = new SysUser();
        sysUser.setLoginIp(ip);
        sysUser.setLoginTime(LocalDateTime.now());
        sysUser.setUserId(UserUtils.CurrentUser().getSysUser().getUserId());

        sysUserService.updateById(sysUser);

        return "index";
    }

    /**
     * 验证码
     *
     * @param session  会话对象
     * @param response 相应对象
     * @throws IOException 异常对象
     */
    @GetMapping("/captcha")
    public void captcha(HttpSession session, HttpServletResponse response) throws IOException {

        RandomGenerator randomGenerator = new RandomGenerator("234578acdefghjkmnprstwxyzABCDEFGHJKMNQPRSTWXYZ", 4);
        //生成验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 40, 4, 50);
        captcha.setGenerator(randomGenerator);
//        captcha.setFont(new Font(Font.DIALOG,Font.BOLD,25));
        captcha.createCode();
        log.info("验证码：" + captcha.getCode());

        //存入session
        session.setAttribute("captcha", captcha.getCode());
        captcha.write(response.getOutputStream());
    }

    @GetMapping("test")
    public void redirect(HttpServletResponse response) throws IOException {

        response.sendRedirect("https://appx.wodexuzhou.com/system/download");
    }

}
